package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlLlzdxdmdjlbBsDetail;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlLlzdxdmdjlbBsDetailVo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlLlzdxdmdjlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlLlzdxdmdjlbBsInfoBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlLlzdxdmdjlbBsInfoVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlLlzdxdmdjlbBsInfo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlLlzdxdmdjlbBsInfoMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlLlzdxdmdjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料理论最大相对密度试验检测记录(真空法)Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlLlzdxdmdjlbBsInfoServiceImpl implements IReportLqhhlLlzdxdmdjlbBsInfoService {

    private final ReportLqhhlLlzdxdmdjlbBsInfoMapper baseMapper;
    private final ReportLqhhlLlzdxdmdjlbBsDetailMapper detailMapper;

    /**
     * 查询沥青混合料理论最大相对密度试验检测记录(真空法)
     */
    @Override
    @FIllBasicInfo
    public ReportLqhhlLlzdxdmdjlbBsInfoVo queryById(Long excelId){
        ReportLqhhlLlzdxdmdjlbBsInfoVo reportLqhhlLlzdxdmdjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<ReportLqhhlLlzdxdmdjlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<ReportLqhhlLlzdxdmdjlbBsDetail>().eq(ReportLqhhlLlzdxdmdjlbBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            reportLqhhlLlzdxdmdjlbBsInfoVo.setDetailList(detailList);
        }
        return reportLqhhlLlzdxdmdjlbBsInfoVo;
    }

    /**
     * 查询沥青混合料理论最大相对密度试验检测记录(真空法)列表
     */
    @Override
    public TableDataInfo<ReportLqhhlLlzdxdmdjlbBsInfoVo> queryPageList(ReportLqhhlLlzdxdmdjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlLlzdxdmdjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlLlzdxdmdjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料理论最大相对密度试验检测记录(真空法)列表
     */
    @Override
    public List<ReportLqhhlLlzdxdmdjlbBsInfoVo> queryList(ReportLqhhlLlzdxdmdjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqhhlLlzdxdmdjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlLlzdxdmdjlbBsInfo> buildQueryWrapper(ReportLqhhlLlzdxdmdjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlLlzdxdmdjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqhhlLlzdxdmdjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqhhlLlzdxdmdjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqhhlLlzdxdmdjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqhhlLlzdxdmdjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqhhlLlzdxdmdjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqhhlLlzdxdmdjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqhhlLlzdxdmdjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqhhlLlzdxdmdjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqhhlLlzdxdmdjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqhhlLlzdxdmdjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqhhlLlzdxdmdjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqhhlLlzdxdmdjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqhhlLlzdxdmdjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqhhlLlzdxdmdjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqhhlLlzdxdmdjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqhhlLlzdxdmdjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqhhlLlzdxdmdjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqzljph()), ReportLqhhlLlzdxdmdjlbBsInfo::getLiqzljph, bo.getLiqzljph());
        lqw.eq(bo.getLiqbz() != null, ReportLqhhlLlzdxdmdjlbBsInfo::getLiqbz, bo.getLiqbz());
        lqw.eq(bo.getBanhwd() != null, ReportLqhhlLlzdxdmdjlbBsInfo::getBanhwd, bo.getBanhwd());
        lqw.eq(StringUtils.isNotBlank(bo.getHunhllx()), ReportLqhhlLlzdxdmdjlbBsInfo::getHunhllx, bo.getHunhllx());
        lqw.eq(bo.getShuidmd() != null, ReportLqhhlLlzdxdmdjlbBsInfo::getShuidmd, bo.getShuidmd());
        lqw.eq(StringUtils.isNotBlank(bo.getFuyrqlx()), ReportLqhhlLlzdxdmdjlbBsInfo::getFuyrqlx, bo.getFuyrqlx());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqhhlLlzdxdmdjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportLqhhlLlzdxdmdjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportLqhhlLlzdxdmdjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportLqhhlLlzdxdmdjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportLqhhlLlzdxdmdjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqhhlLlzdxdmdjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqhhlLlzdxdmdjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqhhlLlzdxdmdjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增沥青混合料理论最大相对密度试验检测记录(真空法)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqhhlLlzdxdmdjlbBsInfoBo bo) {
        ReportLqhhlLlzdxdmdjlbBsInfo add = BeanUtil.toBean(bo, ReportLqhhlLlzdxdmdjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<ReportLqhhlLlzdxdmdjlbBsDetail> details = new ArrayList<>(10);
            for (int i=1;i<=10;i++ ){
                details.add(new ReportLqhhlLlzdxdmdjlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改沥青混合料理论最大相对密度试验检测记录(真空法)
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportLqhhlLlzdxdmdjlbBsInfoBo bo) {
        ReportLqhhlLlzdxdmdjlbBsInfo update = BeanUtil.toBean(bo, ReportLqhhlLlzdxdmdjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportLqhhlLlzdxdmdjlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlLlzdxdmdjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料理论最大相对密度试验检测记录(真空法)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
